package handler

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"shop/api/requset"
	"shop/internal/dao"
	"shop/internal/model"
)

// 添加
func AddGoods(c *gin.Context) {
	//接收参数
	var req requset.AddGoodsReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}

	//业务逻辑

	result := dao.Create(&model.Goods{
		Title:  req.Title,
		Price:  req.Price,
		Info:   req.Info,
		Stock:  req.Stock,
		UserId: c.GetUint("userId"),
	})
	if !result {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "添加失败",
			"data": false,
		})
		return
	}

	//返回参数
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "添加成功",
		"data": true,
	})
	return

}

// 修改
func UpdateGoods(c *gin.Context) {
	//接收参数
	var req requset.UpdateGoodsReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}

	//业务逻辑
	//1.验证商品存在
	var goods model.Goods
	dao.GetOneById(req.Id, &goods)
	if goods.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "商品不存在",
			"data": nil,
		})
		return
	}

	//2.修改权限
	if goods.UserId != c.GetUint("userId") {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "没有修改权限",
			"data": nil,
		})
		return
	}

	//3.修改
	dao.Update(&model.Goods{
		Id:     req.Id,
		Title:  req.Title,
		Price:  req.Price,
		Info:   req.Info,
		Stock:  req.Stock,
		UserId: c.GetUint("userId"),
	})

	//返回参数
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "修改成功",
		"data": true,
	})
	return

}

// 展示
func ListGoods(c *gin.Context) {
	//接收参数
	var req requset.ListGoodsReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}

	//业务逻辑
	var goods []model.Goods
	result := dao.GetListByFields(&model.Goods{
		Id:     req.Id,
		Title:  req.Title,
		Price:  req.Price,
		Info:   req.Info,
		Stock:  req.Stock,
		UserId: c.GetUint("userId"),
	}, &goods)

	if !result {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "商品展示失败",
			"data": false,
		})
		return
	}

	//返回参数
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "商品展示成功",
		"data": goods,
	})
	return

}

// 分页
func PageGoods(c *gin.Context) {
	//接收参数
	var req requset.PageGoodsReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}

	//业务逻辑
	pageDto := dao.PageDto{
		Page:     req.Page,
		PageSize: req.PageSize,
		OrderBy:  "created_at desc",
	}

	var goods []model.Goods
	paginate, err := dao.Paginate(&model.Goods{
		Id:     req.Id,
		Title:  req.Title,
		UserId: c.GetUint("userId"),
	}, pageDto, &goods)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "商品分页展示失败",
			"data": err.Error(),
		})
		return
	}

	//返回参数
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "商品分页展示成功",
		"data": paginate,
	})
	return

}

// 详情
func DetailGoods(c *gin.Context) {
	//接收参数
	var req requset.DetailGoodsReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}

	//业务逻辑
	var goods []model.Goods
	result := dao.GetOneById(req.Id, &goods)

	if !result {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "商品详情展示失败",
			"data": false,
		})
		return
	}

	//返回参数
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "商品详情展示成功",
		"data": goods,
	})
	return
}

// 删除
func DelGoods(c *gin.Context) {
	//接收参数
	var req requset.DelGoodsReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}

	//业务逻辑
	result := dao.Delete(&model.Goods{
		Id: req.Id,
	})

	if !result {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "商品评论删除失败",
			"data": false,
		})
		return
	}

	//返回参数
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "商品评论删除成功",
		"data": true,
	})
	return
}
